#include<stdio.h>
#include<stdbool.h>
#include <string.h>


// 这是管理界面的初始定义处


// #define IN_WIDETH 42
const long IN_WIDETH = 42;


// 定义初始化数据结构
// book的结构体定义
typedef struct book_struct
{
    char name[30];
    char writer[30];
    char position[10];
    // unsigned int remaining;
    char borrow_stu[15];
    char borrow_call[11];
    bool status : 1;
    long int id; // 用于记录当前图书在数组中的位置
} book;


// 状态显示函数
char *check_status(book book)
{
    if (book.status == 1)
    {
        return "available";
    }
    else
    {
        return "unavailable";
    }
}

// 数据库初始化
book allBooks[100] = {};


long rank = 1;
long *rank_place = &rank;

// 暂存区
book wait;


// 用户的选择
int choice;
int choice1;
char input[30] = "";




// 居中打印函数
void center_printf(char *input)
{
    long strlength = strlen(input);

    long before_space = (IN_WIDETH - strlength) / 2;

    long after_space = IN_WIDETH - strlength - before_space;

    for (long i = 0; i < before_space; i++)
    {
        printf(" ");
    }

    printf("%s", input);

    for (long i = 0; i < after_space; i++)
    {
        printf(" ");
    }
    printf("\n");
}

// 字符串固定打印
void fixed_printf(char *key, char *input)
{

    for (int i = 0; i < 10; i++)
        printf(" ");
    
	printf("%s", key);
    printf(":  ");
    printf("%s", input);
    printf("\n");
}

// 整型固定打印
void fixed_printf_int(char *key, long int input)
{

    for (int i = 0; i < 10; i++)
        printf(" ");
    printf("%s", key);
    printf(":  ");
    printf("%ld", input);
    printf("\n");
}












// 主界面提前声明
int admin();


// 交叉引用，提前声明
void editBook();
void editchoice();
void deleteBook();



// 管理界面显示
int toLibrarian()
{
	printf(" ========================================== \n");
	printf("              欢迎来到管理界面              \n");
	printf(" ****************************************** \n");
	printf("             1,查看原有图书信息             \n");
	printf("             2,修改原有图书信息             \n");
	printf("             3,增添新的图书                 \n");
	printf("             4,删除图书信息                 \n");
	printf(" ========================================== \n");
	printf("             9,回到主界面                   \n");
	printf("             0,退出系统                     \n");
	printf(" ========================================== \n");
}

// 图书总信息显示
void Bookimformationplus(book book)
{
	printf(" ========================================== \n"); // 长42
	center_printf(book.name);
	printf(" ****************************************** \n");
	fixed_printf("书名", book.name);
	fixed_printf("作者", book.writer);
	fixed_printf("状态", check_status(book));
	if (book.status == 0)
	{
		fixed_printf("借阅者", book.borrow_stu);
		fixed_printf("借阅人电话", book.borrow_call);
	}
	else
	{
		fixed_printf("所在位置", book.position);
	}
	printf(" ========================================== \n");
	printf("             0,回到管理界面                 \n");
	printf(" ========================================== \n");
}

// 创建图书
book createBook()
{

	char name[30];
	char writer[30];
	char position[10];

	printf("请输入图书名称：");
	scanf("%s", name);
	printf("请输入图书作者：");
	scanf("%s", writer);
	printf("请输入图书位置：");
	scanf("%s", position);

	strcpy(allBooks[rank].name, name);
	strcpy(allBooks[rank].writer, writer);
	strcpy(allBooks[rank].position, position);
	strcpy(allBooks[rank].borrow_stu, "无");
	strcpy(allBooks[rank].borrow_call, "无");
	allBooks[rank].status = 1;
	allBooks[rank].id = rank;


	return allBooks[rank];
}

// 添加图书
void addBook()
{
	allBooks[rank] = createBook();
	(*rank_place)++;
}

// 在更改图书的信息时的显示函数
void Bookimformationchange(book book)
{
	printf(" ========================================== \n"); // 长42
	center_printf(book.name);
	printf(" ****************************************** \n");
	fixed_printf("1.书名", book.name);
	fixed_printf("2.作者", book.writer);
	if (book.status == 1)
	{
		fixed_printf("3.所在位置", book.position);
	}
	printf(" ========================================== \n");
	printf("             0,回到管理界面                 \n");
	printf(" ========================================== \n");
}


// 图书的信息修改初始界面
void editBook(int choice)
{
	int choice1;
	Bookimformationchange(allBooks[choice]);
	printf("请输入你的选择:");
	scanf("%d", &choice1);

	editchoice(choice, choice1);
	}

// 图书的信息修改选择
void editchoice(int choice, int choice1){
	switch (choice1)
	{
	case 1:
		printf("请输入图书新的书名：");
		scanf("%s", input);
		strcpy(allBooks[choice].name, input);
		printf("\033[2J");
		editBook(choice);
		break;
	case 2:
		printf("请输入图书新的作者：");
		scanf("%s", input);
		strcpy(allBooks[choice].writer, input);
		printf("\033[2J");
		editBook(choice);
		break;
	case 3:
		printf("请输入图书新的所在位置：");
		scanf("%s", input);
		strcpy(allBooks[choice].position, input);
		printf("\033[2J");
		editBook(choice);
		break;
	case 0:
		break;
	default:
		center_printf("输入错误，请重新输入\n");
		scanf("%d", &choice1);
		editchoice(choice, choice1);
		break;
	}
}







void deleteBook()
{
	printf("请输入要删除的图书编号：");
	scanf("%d", &choice);

	if (allBooks[choice].status == 0)
	{
		center_printf("该图书已被借出，无法删除\n");
		deleteBook(); // 重新调用删除函数
	}
	else
	{
	printf(" ========================================== \n"); // 长42
	center_printf(allBooks[choice].name);
	printf(" ****************************************** \n");
	fixed_printf("1.书名", allBooks[choice].name);
	fixed_printf("2.作者", allBooks[choice].writer);
	fixed_printf("3.所在位置", allBooks[choice].position);
	printf(" ========================================== \n"); // 长42
	center_printf("1.确认删除");
	center_printf("2.取消删除");

	printf(" ========================================== \n\n"); // 长42
	printf("请输入你的选择:");
	scanf("%d", &choice1);
	printf("\033[2J");
	switch (choice1)
	{
	case 1:
		printf("\033[2J");  // 输出 ANSI 转义序列以清除屏幕
		printf(" ========================================== \n\n");
		printf("       您确定删除吗？该操作无法撤销\n");
		printf(" ========================================== \n");
		center_printf("1.确认删除");
		center_printf("2.取消删除");
		printf(" ========================================== \n");
		printf("请输入你的选择:");
		scanf("%d", &choice1);
		if (choice1 == 1) {
		strcpy(allBooks[choice].name, "已删除");
		strcpy(allBooks[choice].writer, "已删除");
		strcpy(allBooks[choice].position, "已删除");
		strcpy(allBooks[choice].borrow_stu, "已删除");
		strcpy(allBooks[choice].borrow_call, "已删除");
		allBooks[choice].status = 2;
		allBooks[choice].id = choice;
		printf("\033[2J");
		printf(" ========================================== \n\n");
		center_printf("删除成功");
		printf(" ========================================== \n\n");
		printf("输入任意数返回\n");
		scanf("%d", &choice1);
		admin();
		break;
		} 
		else {
		printf("\033[2J");
		admin();
		break;
		}
	case 2:
		printf("\033[2J");
		admin();
		break;
	}
	}
}










int admin()
{
	toLibrarian();
	printf("\n您好.请输您的选择(1,2,3,4,5,6,7,8,9,0):\n");
	scanf("%d", &choice);


	printf("\033[2J");  // 输出 ANSI 转义序列以清除屏幕


	switch (choice)
	{
	case 1:
		printf("请输入要查询的图书编号：");
		scanf("%d", &choice);
		Bookimformationplus(allBooks[choice]);

		center_printf("输入任意键返回管理界面");
		scanf("%d",&choice);

		printf("\033[2J");  // 输出 ANSI 转义序列以清除屏幕
		admin();
		break;

	case 2:
		printf("请输入要修改的图书编号：");
		scanf("%d", &choice);
		editBook(choice);

		printf("\033[2J");  // 输出 ANSI 转义序列以清除屏幕
		admin();
		break;

	case 3:
		addBook();
		admin();
		break;

	case 4:
		deleteBook();
		break;

	case 0:
		printf("\033[2J");  // 输出 ANSI 转义序列以清除屏幕
		break;

	default:
		center_printf("输入错误，请重新输入\n");
		admin();
		break;
	}
	return 0;
} 

int main(){
	admin();
	return 0;
}
